package core;

import javax.swing.SwingWorker;

import db.Mapper;

public class FindTask extends SwingWorker<Void, Integer>
{
	private ChatNode node;
	private String findName;
	
	FindTask(String name, ChatNode node)
	{
		this.findName = name;
		this.node = node;
	}
	
	public String getFindName()
	{
		return this.findName;
	}
	
	
	@Override
	protected Void doInBackground() throws Exception
	{
		setProgress(0);
		while(!isCancelled() && getProgress()<3)
		{
			try
			{
				Thread.sleep(1000);
				setProgress(getProgress() + 1);
				publish(getProgress());
			}
			catch(InterruptedException ex)
			{
			
			}
		}
		
		return null;
	}
	

	@Override
	protected void done()
	{
		Mapper userbase = node.getUserBase();
		if(!userbase.containsName(findName))
		{
			node.getMainWindow().appendText("Peer \"" + findName + "\" not found.");
		}
		node.endFindTask();
	}
}
